// 引入接口文件
import { deleteConsultant } from './consultantAdminServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defaultState = {
  biz: {
    queryForm: {}
  },
    score:'',
}

// 声明module
const tmpModule = {
  namespace: 'consultantAdminModel',
  // 默认状态
  state: cloneDeep(defaultState),
  // 入口函数(先执行)
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if ('' + tmpModule.pathname === '' + pathname) {
          if(query.productName){
            let biz = {
              queryForm: { productId: query.productId}}
            dispatch({ type: 'updateStore', payload: {biz: biz}});
          }else{
            dispatch({ type: 'updateStore', payload: cloneDeep(defaultState) });
          }
        }
      })
    },
  },

  // 方法列表
  effects: {
    // 设置表单的值到状态机
    *setFormVal({ payload }, { put, call, select }) {
      let { biz } = yield select(d => d[tmpModule.namespace])
      try {
        yield put({
          type: 'updateStore',
          payload: {
            biz: payload
          }
        })
      } catch (e) {
        console.error(e)
      }
    },
    // 删除置业顾问
    *deleteConsultant({ payload }, { put, call, select }) {
      console.log(payload, 'payload value');
      try {
        const { success, rows } = yield call(deleteConsultant, parse(payload))
        if (success) {
          yield put({ type: 'updateStore' })
          payload.cb();
        }
      } catch (e) {
        console.error(e)
      }
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
